﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class TotalProcessRepository
    {
        /// 添加总流程
        public int AddTotalProcess(TotalProcess totalProcess)
        {
            string sql = @"INSERT INTO totalprocess 
                            (TotalProcessName, TotalProcessDescription, TotalProcessVersion, Remarks) 
                           VALUES 
                            (@TotalProcessName, @TotalProcessDescription, @TotalProcessVersion, @Remarks)";

            MySqlParameter[] parameters = {
                new MySqlParameter("@TotalProcessName", totalProcess.TotalProcessName),
                new MySqlParameter("@TotalProcessDescription", totalProcess.TotalProcessDescription),
                new MySqlParameter("@TotalProcessVersion", totalProcess.TotalProcessVersion),
                new MySqlParameter("@Remarks", totalProcess.Remarks)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        /// 删除总流程
        public int DeleteTotalProcess(int totalProcessId)
        {
            string sql = "DELETE FROM totalprocess WHERE TotalProcessId = @TotalProcessId";
            MySqlParameter[] parameters = { new MySqlParameter("@TotalProcessId", totalProcessId) };
            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        /// 更新总流程信息
        public int UpdateTotalProcess(TotalProcess totalProcess)
        {
            string sql = @"UPDATE totalprocess 
                            SET TotalProcessName = @TotalProcessName, 
                                TotalProcessDescription = @TotalProcessDescription, 
                                TotalProcessVersion = @TotalProcessVersion, 
                                Remarks = @Remarks 
                          WHERE TotalProcessId = @TotalProcessId";

            MySqlParameter[] parameters = {
                new MySqlParameter("@TotalProcessName", totalProcess.TotalProcessName),
                new MySqlParameter("@TotalProcessDescription", totalProcess.TotalProcessDescription),
                new MySqlParameter("@TotalProcessVersion", totalProcess.TotalProcessVersion),
                new MySqlParameter("@Remarks", totalProcess.Remarks),
                new MySqlParameter("@TotalProcessId", totalProcess.TotalProcessId)
            };

            return DBHelper.ExecuteNonQuery(sql, parameters);
        }

        /// 查询所有总流程
        public List<TotalProcess> GetAllTotalProcesses()
        {
            string sql = "SELECT * FROM totalprocess";
            return DBHelper.ExecuteQuery(sql, null, MapTotalProcess);
        }

        /// 根据ID查询单个总流程
        public TotalProcess GetTotalProcessById(int totalProcessId)
        {
            string sql = "SELECT * FROM totalprocess WHERE TotalProcessId = @TotalProcessId";
            MySqlParameter[] parameters = { new MySqlParameter("@TotalProcessId", totalProcessId) };
            var totalProcesses = DBHelper.ExecuteQuery(sql, parameters, MapTotalProcess);
            return totalProcesses.FirstOrDefault();
        }

        /// 映射数据库查询结果到 TotalProcess 对象
        private TotalProcess MapTotalProcess(MySqlDataReader reader)
        {
            return new TotalProcess
            {
                TotalProcessId = (int)reader["TotalProcessId"],
                TotalProcessName = reader["TotalProcessName"].ToString(),
                TotalProcessDescription = reader["TotalProcessDescription"] is DBNull ? null : reader["TotalProcessDescription"].ToString(),
                TotalProcessVersion = reader["TotalProcessVersion"].ToString(),
                Remarks = reader["Remarks"] is DBNull ? null : reader["Remarks"].ToString()
            };
        }
    }
}